package com.tbit.main.controller;


import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.ControlType;
import com.tbit.main.constant.ControlTypeConstant;
import com.tbit.main.constant.UserConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.MainControlLog;
import com.tbit.main.pojo.OrderLog;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 远程控制日志
 *
 * @author Leon
 * 2019年11月23日 上午11:29:55
 */
@RestController
@RequestMapping("/mainControlLog")
@Slf4j
public class MainControlLogController {
    @Autowired
    private MainControlLogService mainControlLogService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private OrderLogService orderLogService;
    @Autowired
    private AccountUserService accountUserService;

    /**
     * 查询统计
     */
    @RequestMapping("/queryMain")
    public JsonResult getByTime(String token, Integer accountId, Integer userMainId, String userCode, String startTime, String endTime, Integer rowCount, Integer pageNO) {
        AccountUser accountUserToken = tokenService.getAccount(token);
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");

        /*默认值*/
        if (rowCount == null || rowCount < 1) {
            rowCount = 10;
        }
        if (pageNO == null || pageNO < 1) {
            pageNO = 1;
        }

        if (userCode != null) {
            /**查询设备信息*/
            Machine machine = machineService.getByUseCode(userCode);
            if (machine == null) {
                throw new BaseException("Machine.notExist");
            }

            /**设备权限判断*/
            machineService.auth(accountUserToken.getAgents(), machine.getMachineId());
        }

        /*获取总数*/
        Integer count = mainControlLogService.queryCount(accountId, userMainId, userCode, startTime, endTime);

        List<MainControlLog> mainControlLogs = mainControlLogService.queryPage(accountId, userMainId, userCode, startTime, endTime, rowCount, pageNO);

        JsonResult result = JsonResult.succ(mainControlLogs);

        result.setTotal(count);

        return result;
    }

    /**
     * 查询统计
     */
    @RequestMapping("/query")
    public JsonResult getByTimeOrderLog(String token, Integer accountId, Integer userMainId, String userCode, String startTime,
                                        String endTime, Integer rowCount, Integer pageNO) {
        AccountUser accountUserToken = tokenService.getAccount(token);
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");

        /*默认值*/
        if (rowCount == null || rowCount < 1) {
            rowCount = 10;
        }
        if (pageNO == null || pageNO < 1) {
            pageNO = 1;
        }

        Integer userType = null;

        if (userCode != null) {
            /**查询设备信息*/
            Machine machine = machineService.getByUseCode(userCode);
            if (machine == null) {
                throw new BaseException("Machine.notExist");
            }

            /**设备权限判断*/
            machineService.auth(accountUserToken.getAgents(), machine.getMachineId());
        } else {
            AccountUser accountUser = accountUserService.getAccountUserById(userMainId);

            /**账号类型转换*/
            if (accountUser.getSubRoleType() == 0) {//品牌、加盟商、区域代理商
                userType = accountUser.getAccountType();
            } else if (accountUser.getSubRoleType() == 1) {//运维管理员
                userType = UserConstant.ACCOUNT_MAINTAIN_MANAGER;
            } else if (accountUser.getSubRoleType() == 2) {//运维人员
                userType = UserConstant.ACCOUNT_MAINTAIN;
            } else {
                userType = accountUser.getSubRoleType();
            }

        }

        /*获取总数*/
        Integer count = orderLogService.queryCount(accountId, userMainId, userCode, startTime, endTime, userType);

        List<OrderLog> orderLogs = orderLogService.queryPage(accountId, userMainId, userCode, startTime, endTime, rowCount, pageNO, userType);
        List<MainControlLog> mainControlLogs = new ArrayList<>();
        for (OrderLog orderLog : orderLogs) {
            MainControlLog mainControlLog = new MainControlLog();
            mainControlLog.setMachineNO(orderLog.getMachineNo());
            mainControlLog.setOperaTime(orderLog.getOpTime());
            if (orderLog.getOrderContent() == null) {
                orderLog.setOrderContent(orderLog.getOpType() + "");
            }

            if (orderLog.getOrderType().equals(ControlTypeConstant.CONTROL_TYPE_PHOTO)) {
                mainControlLog.setRemark(ControlType.MACHINE_CONTROL_COERCE_PHOTOGRAPH_N);
            } else if (orderLog.getOrderType().equals(ControlTypeConstant.CONTROL_TYPE_UP_PIC)) {
                mainControlLog.setRemark(ControlType.MACHINE_CONTROL_COERCE_PICTURE_N);
            } else {
                switch (orderLog.getOrderContent()) {
                    case "1":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_LOCK_N);
                        break;
                    case "11":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_UNLOCK_N);
                        break;
                    case "15":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_BAT_UNLOCK_N);
                        break;
                    case "16":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_BAT_LOCK_N);
                        break;
                    case "8":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_DW_N);
                        break;
                    case "9":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_FIND_N);
                        break;
                    case "22":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_OPEN_TK_N);
                        break;
                    case "23":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_CLOSE_TK_N);
                        break;
                    case "48":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_CLOSE_ZTTZ_N);
                        break;
                    case "74":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_COERCE_HRESTE_N);
                        break;
                    case "71":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_COERCE_LOCK_N);
                        break;
                    case "12":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_COERCE_UNLOCK_N);
                        break;
                    case "3":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_RESTART_N);
                    case "253":
                        mainControlLog.setRemark(ControlType.MACHINE_CONTROL_FORCED_NETWORKING_N);
                        break;

                }
            }


            mainControlLog.setUserCode(orderLog.getUserCode());
            mainControlLog.setUserMainId(orderLog.getOpId());
            if (Objects.nonNull(orderLog.getRemark()) && orderLog.getRemark().contains(":")) {
                int index = orderLog.getRemark().lastIndexOf(":");
                mainControlLog.setUserMainName(orderLog.getRemark().substring(index + 1));
            } else {
                mainControlLog.setUserMainName(orderLog.getRemark());
            }
            mainControlLog.setAccountId(accountId);
            mainControlLogs.add(mainControlLog);
        }

        JsonResult result = JsonResult.succ(mainControlLogs);
        result.setTotal(count);
        return result;
    }
}